// pages/ucenter/index/index.js
const user = require('../../../utils/user')
const util = require('../../../utils/util')
const api =require('../../../config/api')
var app =getApp()
Page({
  data: {
    userInfo:{
      nickName:'点击登录',
      avatarUrl:'https://img1.baidu.com/it/u=2327718586,410319763&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'
    },
    flag:false
  },
  // 点击登录按钮 去登录页面
  goLogin(){
    if(app.globalData.hasLogin){
      return
    }
    wx.navigateTo({
      url:"/pages/auth/login/login"
    })
  },
  logout(){
    wx.showModal({
      title: '',
      content: '退出登录？',
      success:function(res){
        console.log(res);
        if(res.confirm){
          console.log(123);
          util.request(api.AuthLogout,'','post').then(res=>{
            console.log(res);
          })
          app.globalData.hasLogin=false
          wx.removeStorageSync('userInfo')
          wx.removeStorageSync('token')
          wx.reLaunch({
            url: '/pages/auth/login/login',
          })
        }
        
      }
      
    })
  },

  onShow:function(){
    // 检查否登录
    user.checkLogin().then(res=>{
        let userInfo =wx.getStorageSync('userInfo')
        this.setData({
          userInfo:userInfo,
          hasLogin:true
        })
    }).catch(err=>{
      console.log(1);
    })
  },
  onLoad() {
    let that =this
    let flag =wx.getStorageSync('userInfo')
    if(flag.nickName){
      that.setData({
        flag:true
      })
    }
  },

})